<?php
require('./constrl/lookPage.php');
pdlook();
require('../model/conn.php');
// 处理删除文章请求
if (isset($_GET['deleteId'])) {
  $articleId = intval($_GET['deleteId']);
  // 删除指定文章
  $sql = "DELETE FROM tbl_article WHERE article_id=$articleId";
  $rows = exec_update($sql);
  if ($rows > 0) {
    echo "<script>alert('文章已删除');window.location.href='article_manage.php';</script>";
    exit;
  } else {
    echo "<script>alert('文章删除失败');window.location.href='article_manage.php';</script>";
    exit;
  }
}
// 查询所有文章
$articles = exec_select("SELECT article_id, title, view_count, uid FROM tbl_article");
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>说点吧 - 文章管理</title>
  <link rel="stylesheet" href="../style/css/bootstrap.min.css">
  <link rel="stylesheet" href="../style/css/adminIndex.css">
  <link rel="stylesheet" href="../style/css/font-face.css">
</head>

<body>
  <!-- 导航栏 -->
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <span class="navbar-brand">说点吧 后台管理</span>
      </div>
      <ul class="nav navbar-nav">
        <li><a href="index.php">后台首页</a></li>
        <li><a href="user_manage.php">用户管理</a></li>
        <li><a href="user_upgrade.php">账户升级</a></li>
        <li><a href="board_manage.php">板块管理</a></li>
        <li class="active"><a href="article_manage.php">管理文章</a></li>
        <li>
          <?php
          if (isset($_SESSION['hdb']) && trim($_SESSION['hdb']) != '') {
            echo "<span> $_SESSION[hdb]</span><a href='view/adminOut.php'>退出管理</a>";
          }
          ?>
        </li>
      </ul>
    </div>
  </nav>

  <div class="container">
    <h2>文章管理</h2>
    <!-- 文章列表表格 -->
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>文章ID</th>
          <th>标题</th>
          <th>浏览量</th>
          <th>用户ID</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($articles as $article) { ?>
          <tr>
            <td><?php echo $article['article_id']; ?></td>
            <td><?php echo $article['title']; ?></td>
            <td><?php echo $article['view_count']; ?></td>
            <td><?php echo $article['uid']; ?></td>
            <td>
              <a href="?deleteId=<?php echo $article['article_id']; ?>" class="btn btn-sm btn-danger"
                onclick="return confirm('确定删除该文章吗？');">删除</a>
            </td>
          </tr>
        <?php } ?>
      </tbody>
    </table>
  </div>

  <script src="../style/js/jquery.min.js"></script>
  <script src="../style/js/bootstrap.min.js"></script>
</body>

</html>